﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ingebet.Model.Domain;
using Ingebet.Model.Enum;
using Ingebet.Repository;
using Ingebet.Repository.Contract;

namespace Ingebet.Model
{
    public class PariModel : BaseModel<Pari , IPariRepository , int>
    {
        private CompetitionRepository RepoCompetition = new CompetitionRepository();
        private RencontreRepository RepoRencontre = new RencontreRepository();

        public PariModel()
            : base(new PariRepository())
        {
        }

        public bool GetPariByUtilIdRencontreID(int utilID , int rencID)
        {
            return _repository.GetPariByUtilIdRencontreID(utilID , rencID);
        }

        public IList<Pari> GetByUtilId(int utilID)
        {
            return _repository.GetByUtilId(utilID);
        }

        public IList<VueMatch> GetVuePariAvecResultByUtilID(int utilID)
        {
            List<VueMatch> result = new List<VueMatch>();
            var lstVue = GetByUtilId(utilID);
            foreach (var item in lstVue)
            {
                string parisResultat;
                switch (item.Resultat)
                {
                    case 0:
                        parisResultat = "Gain";
                        break;

                    case 1:
                        parisResultat = "Egalite";
                        break;

                    default:
                        parisResultat = "Perte";
                        break;
                }

                var ren = RepoRencontre.GetByID(item.RencontreID);
                if (ren.Score != string.Empty)
                {
                    string rencontreResultat;
                    switch (ren.Resultat)
                    {
                        case 0:
                            rencontreResultat = "Gain";
                            break;

                        case 1:
                            rencontreResultat = "Egalite";
                            break;

                        default:
                            rencontreResultat = "Perte";
                            break;
                    }

                    result.Add(new VueMatch()
                    {
                        Competition = ren.CompetitionMatch.Nom ,
                        RencontreID = item.RencontreID ,
                        DateMatch = ren.DateRencontre ,
                        Equipe1 = ren.Equipe1.Nom ,
                        Equipe2 = ren.Equipe2.Nom ,
                        CoteGain = ren.CoteMatch.CoteGain ,
                        CoteEgalite = ren.CoteMatch.CoteEgalite ,
                        CotePerte = ren.CoteMatch.CotePerte ,
                        PariResultat = parisResultat ,
                        Montant = item.MontantPari.ToString() ,
                        DatePari = item.DatePari ,
                        Resultat = rencontreResultat
                    });
                }
            }
            return result;
        }

        public IList<Pari> UpdatePariByRencontre(int rencID)
        {
            return _repository.UpdatePariByRencontre(rencID);
        }

        public IList<VueMatch> GetVuePariByUtilId(int utilID)
        {
            List<VueMatch> result = new List<VueMatch>();
            var lstVue = GetByUtilId(utilID);
            foreach (var item in lstVue)
            {
                string res;
                switch (item.Resultat)
                {
                    case 0:
                        res = "Gain";
                        break;

                    case 1:
                        res = "Egalite";
                        break;

                    default:
                        res = "Perte";
                        break;
                }

                var ren = RepoRencontre.GetByID(item.RencontreID);
                if (ren.Score == string.Empty)
                {
                    result.Add(new VueMatch()
                    {
                        Competition = ren.CompetitionMatch.Nom ,
                        RencontreID = item.RencontreID ,
                        DateMatch = ren.DateRencontre ,
                        Equipe1 = ren.Equipe1.Nom ,
                        Equipe2 = ren.Equipe2.Nom ,
                        CoteGain = ren.CoteMatch.CoteGain ,
                        CoteEgalite = ren.CoteMatch.CoteEgalite ,
                        CotePerte = ren.CoteMatch.CotePerte ,
                        Resultat = res ,
                        Montant = item.MontantPari.ToString() ,
                        DatePari = item.DatePari
                    });
                }
            }
            return result;
        }
    }
}